﻿

using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.SystemSet;
using SmartWMS.EntityFramework;
using SmartWMS.Server.Repository;
using SmartWMS.Server.SystemSets.RoleServices.Dto;

namespace SmartWMS.Server.SystemSets.RoleServices
{
    public class RoleSerivce : IRoleSerivce
    {
        private readonly IRepository<Role> _roleRepository;
        private readonly IRepository<RolePermissions> repository;
        private readonly IRepository<Permissions> _perRepository;
        private readonly SmartWMSContext _SmartHospitalContext;
        public RoleSerivce(IRepository<Role> roleRepository, IRepository<RolePermissions> repository, SmartWMSContext smartHospitalContext, IRepository<Permissions> perRepository)
        {
            _roleRepository = roleRepository;
            this.repository = repository;
            _SmartHospitalContext = smartHospitalContext;
            _perRepository = perRepository;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role">要添加的数据</param>
        /// <returns>返回状态</returns>
        public async Task<Response<bool>> AddRoleAsyne(RoleDto role)
        {
            try
            {
                var stu = role.MapTo<Role>();
                var result = await _roleRepository.InsertAsync(stu);
                return result > 0 ? Response<bool>.Ok(result > 0) :
                     Response<bool>.Fail(result > 0);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除角色禁用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DelRoleDtoAsyne(RoleDto input)
        {
            try
            {
                var list = input.MapTo<Role>();
                var result = await _roleRepository.DelState(list);
                return result > 0 ? Response<bool>.Ok(result > 0) :
                     Response<bool>.Fail(result > 0);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns>查询列表</returns>
        public async Task<Response<List<RoleDto>>> GetRoleDtoAsyne(string? name)
        {
            try
            {
                var list = await _roleRepository.GetAllListAsync();
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(t => t.RoleName.Contains(name)).ToList();
                }
                var result = list.MapToList<RoleDto>();
                return Response<List<RoleDto>>.Ok(result);
            }
            catch (Exception)
            {
                throw;
            }
        }



        /// <summary>
        /// 反填角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Response<RoleDto> RackRoleDto(int id)
        {
            try
            {
                var list = _roleRepository.GetListQuery().FirstOrDefault(t => t.Id == id);
                var result = list.MapTo<RoleDto>();
                return Response<RoleDto>.Ok(result);
            }
            catch (Exception)
            {
                throw;
            }

        }
        /// <summary>
        /// 编辑角色列表
        /// </summary>
        /// <param name="input">要编辑的列表</param>
        /// <returns>返回数据</returns>
        public async Task<Response<bool>> UpdRoleAsync(RoleDto input)
        {
            try
            {
                var list = input.MapTo<Role>();
                var result = await _roleRepository.UpdateAsync(list);
                return result > 0 ? Response<bool>.Ok(result > 0) :
                     Response<bool>.Fail(result > 0);
            }
            catch (Exception)
            {
                throw;
            }
        }




        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="rolePermission"></param>
        /// <returns></returns>
        public async Task<Response<bool>> AddRolePermissionAsync(RoleInput rolePermission)
        {
            var list = _roleRepository.AddRolePermissionAsync(rolePermission.RoleID, rolePermission.PermissionsID);
            return await list != null ? Response<bool>.Ok(list != null) :
                     Response<bool>.Fail(list != null);
        }
        /// <summary>
        /// 反填角色权限（树形）
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public async Task<List<int>> SelRolePermissionAsync(RoleInput rolePermission)
        {
            var list = await _roleRepository.SelRolePermissionAsync(rolePermission.RoleID);
            return list;
        }

        /// <summary>
        /// 树形菜单
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public List<PermissionDto> GetRoleMoent(int pid)
        {
            try
            {
                var list = _perRepository.GetListQuery().Where(t => t.PID == pid).ToList();

                var ment = list.Select(t => new PermissionDto
                {
                    value = t.Id,
                    Pid = t.PID,
                    name = t.Name,
                    children = GetRoleMoent(t.Id).Count() > 0 ? GetRoleMoent(t.Id) : null
                }).ToList();
                return ment;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }


    #region
    /// <summary>
    /// 分配权限
    /// </summary>
    /// <param name="roleInput"></param>
    /// <returns></returns>
    //public async Task<Response<bool>> AddPermissionRoleAsync(RoleInput input)
    //    {
    //        var roleId = input.RoleID;
    //        var premissionId = input.PermissionsID;
    //        var i = 0;
    //        var rolePermissions = repository.GetAllListAsync().Result;
    //        rolePermissions = rolePermissions.Where(x => x.RoleID == roleId).ToList();
    //        if (rolePermissions.Any())
    //        {
    //            i = await repository.DeleteAsync(rolePermissions);
    //        }
    //        if (!string.IsNullOrEmpty(premissionId))
    //        {
    //            var rolePermissionList = new List<RolePermissions>();
    //            var premissionIdList = premissionId.Split(',');
    //            foreach (var item in premissionIdList)
    //            {
    //                rolePermissionList.Add(new RolePermissions()
    //                {
    //                    RoleID=roleId,
    //                    PermissionsID=Convert.ToInt32(item)
    //                });
    //            }
    //            i = await repository.InsertAsync(rolePermissionList);
    //        }
    //        return i > 0 ? Response<bool>.Ok(true) : Response<bool>.Ok(false);

    #region
    //using (var tran = _SmartHospitalContext.Database.BeginTransaction())
    //{
    //    try
    //    {
    //        var list = _SmartHospitalContext.Set<RolePermissions>().Where(t => t.RoleID == rid).ToList();
    //        if (list != null)
    //        {
    //            _SmartHospitalContext.Set<RolePermissions>().RemoveRange(list);
    //            _SmartHospitalContext.SaveChanges();
    //        }
    //        var PermissionId = pid.Split(',');
    //        List<RolePermissions> rolePermissions = new List<RolePermissions>();
    //        foreach (var item in PermissionId)
    //        {
    //            var rolePermission = new RolePermissions();
    //            rolePermission.RoleID = rid;
    //            rolePermission.PermissionsID = Convert.ToInt32(item);
    //            rolePermissions.Add(rolePermission);

    //        }
    //        await _SmartHospitalContext.Set<RolePermissions>().AddRangeAsync(rolePermissions);
    //        return await _SmartHospitalContext.SaveChangesAsync();
    //    }
    //    catch (Exception)
    //    {

    //        throw;
    //    }
    //}
    #endregion
}
/// <summary>
/// 角色管理
/// </summary>
/// <returns></returns>
//public async Task<Response<List<RoleDto>>> GetRoleListAsync()
//{
//    var result = await _roleRepository.GetAllListAsync();
//    var rolelist = result.MapToList<RoleDto>();
//    return Response<List<RoleDto>>.Ok(rolelist);
//}
#endregion

